<?php
//长度截取
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){ 
    if(function_exists("mb_substr")){ 
        if($suffix){
             return mb_substr($str, $start, $length, $charset)."..."; 
        }else{
             return mb_substr($str, $start, $length, $charset); 
        }
    }else if(function_exists('iconv_substr')) { 
        if($suffix){
            return iconv_substr($str,$start,$length,$charset)."...";    
        }else{
            return iconv_substr($str,$start,$length,$charset);
        }      
    } 
    $re['utf-8']   = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"; 
    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/"; 
    $re['gbk']    = "/[x01-x7f]|[x81-xfe][x40-xfe]/"; 
    $re['big5']   = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"; 
    preg_match_all($re[$charset], $str, $match); 
    $slice = join("",array_slice($match[0], $start, $length)); 
    if($suffix){return $slice."…";} 
    return $slice;
}

function mdate($time = NULL) {
    $text = '';
    $time = $time === NULL || $time > time() ? time() : intval($time);
    $t = time() - $time; //时间差 （秒）
    $y = date('Y', $time)-date('Y', time());//是否跨年
    switch($t){
     case 0:
       $text = 'just';
       break;
     case $t < 60:
      $text = $t . 'sec.'; // 一分钟内
      break;
     case $t < 60 * 60:
      $text = floor($t / 60) . 'min.'; //一小时内
      break;
     case $t < 60 * 60 * 24:
      $text = floor($t / (60 * 60)) . 'day'; // 一天内
      break;
     case $t < 60 * 60 * 24 * 3:
     // $text = floor($time/(60*60*24)) ==1 ?'yesterday ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
     $text = date('F,d H:i', $time); //一个月内
      break;
     case $t < 60 * 60 * 24 * 30:
      $text = date('F,d H:i', $time); //一个月内
      break;
     case $t < 60 * 60 * 24 * 365&&$y==0:
      $text = date('F,d', $time); //一年内
      break;
     default:
      $text = date('F,d,Y', $time); //一年以前
      break; 
    }  
    return $text;
}

?>